/*
 * AccountInfoFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.insigma.yanwenlong.bookkeeping.dao.CommonDao;
import com.insigma.yanwenlong.bookkeeping.pojo.Account;
import com.insigma.yanwenlong.bookkeeping.service.AccountService;
import com.insigma.yanwenlong.bookkeeping.service.AccountServiceImpl;

/**
 *
 * @author  __USER__
 */
public class AccountInfoFrame extends javax.swing.JFrame {
	private AccountService accountService = new AccountServiceImpl();

	private DefaultTableModel defaultTableModel;

	/** Creates new form AccountInfoFrame */
	public AccountInfoFrame() {
		//设置标题
		setTitle("账户管理");
		//初始化界面
		initComponents();
		//居中显示
		setLocationRelativeTo(null);
		//设置此窗体关闭而不会关闭父窗体
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		//将账户信息显示在界面上
		getAccountInfo();
	}

	private void getAccountInfo() {
		try {
			List<Account> accounts = accountService.findaAccounts();
			defaultTableModel = (DefaultTableModel) tblAccountInfo.getModel();
			for (Account account : accounts) {
				defaultTableModel.addRow(new Object[] { account.getId(),
						account.getName(), account.getCurrAccount(),
						account.getCreateTime(), account.getUpdateTime(),
						account.getNote() });
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		btnUpdate = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		tblAccountInfo = new javax.swing.JTable();
		btnAddAccount = new javax.swing.JButton();
		btnUpdateAccount = new javax.swing.JButton();
		btnDeleteAccount = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		btnUpdate.setText("\u5237\u65b0");
		btnUpdate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnUpdateActionPerformed(evt);
			}
		});

		tblAccountInfo.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				},
				new String[] { "账户编号", "账户名称", "当前余额", "创建日期", "最新更新", "说明" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jScrollPane1.setViewportView(tblAccountInfo);

		btnAddAccount.setText("\u65b0\u589e\u8d26\u6237");
		btnAddAccount.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnAddAccountActionPerformed(evt);
			}
		});

		btnUpdateAccount.setText("\u4fee\u6539\u8d26\u6237");
		btnUpdateAccount.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnUpdateAccountActionPerformed(evt);
			}
		});

		btnDeleteAccount.setText("\u5220\u9664\u8d26\u6237");
		btnDeleteAccount.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnDeleteAccountActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 545,
				Short.MAX_VALUE).addGroup(
				layout.createSequentialGroup().addGap(21, 21, 21).addComponent(
						btnUpdate).addContainerGap(467, Short.MAX_VALUE))
				.addGroup(
						layout.createSequentialGroup().addContainerGap()
								.addComponent(btnAddAccount).addGap(58, 58, 58)
								.addComponent(btnUpdateAccount).addGap(60, 60,
										60).addComponent(btnDeleteAccount)
								.addContainerGap(174, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(btnUpdate)
										.addGap(10, 10, 10)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												164,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																btnAddAccount)
														.addComponent(
																btnUpdateAccount)
														.addComponent(
																btnDeleteAccount))
										.addContainerGap(88, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//点击删除账户
	private void btnDeleteAccountActionPerformed(java.awt.event.ActionEvent evt) {
		if(tblAccountInfo.getSelectedRow() < 0) {
			JOptionPane.showMessageDialog(this, "你没有选择行");
		}else {
			int[] rowCount = tblAccountInfo.getSelectedRows();
			for (int i : rowCount) {
				int id = (Integer)tblAccountInfo.getValueAt(i, 0);
				CommonDao cd = new CommonDao();
				cd.deleteById(id);
			}
		}
	}

	//点击修改账户
	private void btnUpdateAccountActionPerformed(java.awt.event.ActionEvent evt) {
		if (tblAccountInfo.isRowSelected(tblAccountInfo.getSelectedRow())) {
			if(tblAccountInfo.getSelectedRowCount() == 1) {
				new UpdateAccountFrame(tblAccountInfo).setVisible(true);
			}else {
				JOptionPane.showMessageDialog(this, "你选择了多行，请选择一行");
			}
		} else {
			JOptionPane.showMessageDialog(this, "你没有选择行");
		}

	}

	//点击新增账户
	private void btnAddAccountActionPerformed(java.awt.event.ActionEvent evt) {
		new AddAccountFrame().setVisible(true);
	}

	//点击刷新，进行信息刷新
	private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
		//刷新前要先清空表格中的内容
		defaultTableModel.setRowCount(0);
		//将账户信息重新显示在界面上
		getAccountInfo();
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new AccountInfoFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnAddAccount;
	private javax.swing.JButton btnDeleteAccount;
	private javax.swing.JButton btnUpdate;
	private javax.swing.JButton btnUpdateAccount;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable tblAccountInfo;
	// End of variables declaration//GEN-END:variables

}